#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# 读取和保存yaml文件


import yaml
import logging

from commons.models import CaseInfo

logger = logging.getLogger(__name__)


class YamlFile(dict):

    def __init__(self, path):
        super().__init__()  # 让对象按照原来方式完成实例化
        # 接下来完成自定义代码
        self._path = path  # yaml文件路径
        self.load()  # 实例化时，自动加载yaml内容

    def load(self):
        with open(self._path, "r", encoding="utf-8") as f:
            data = yaml.safe_load(f)

        if data:
            # data是字典
            # self是字典
            self.update(data)  # 两个字典内容进行合并

    def save(self):
        with open(self._path, "w", encoding="utf-8") as f:
            yaml.dump(dict(self), f, allow_unicode=True)


if __name__ == '__main__':
    # 文件路径
    path = r"D:\python\TestClass\api_framework\testcases\blackgood_manage\test_1_login.yaml"
    # 文件加载
    file = YamlFile(path)
    # 用例格式校验
    case_info = CaseInfo(**file)
    print(case_info)
